<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" inline label-width="90px">
                <el-form-item label="状态" prop="status">
                    <el-select class="w-[300px]" style="width: 280px" v-model="queryParams.status" clearable
                        placeholder="请选择状态">
                        <el-option label="全部" value=""></el-option>
                        <el-option v-for="(item, index) in dictData.insurance_policy" :key="index" :label="item.name"
                            :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="保单号" prop="order_no">
                    <el-input class="w-[280px]" v-model="queryParams.order_no" clearable placeholder="请输入保单号" />
                </el-form-item>
                <el-form-item label="机型" prop="spec">
                    <el-input class="w-[280px]" v-model="queryParams.spec" clearable placeholder="请输入机型" />
                </el-form-item>
                <el-form-item label="品牌" prop="brand">
                    <el-input class="w-[280px]" v-model="queryParams.brand" clearable placeholder="请输入品牌" />
                </el-form-item>
                <el-form-item label="串号" prop="imei">
                    <el-input class="w-[280px]" v-model="queryParams.imei" clearable placeholder="请输入串号" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <!-- <el-button v-perms="['insurance_policy/add']" type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
新增
</el-button> -->
            <el-button v-perms="['insurance_policy/delete']" :disabled="!selectData.length"
                @click="handleDelete(selectData)"> 删除
            </el-button>
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column label="保单号" prop="order_no" />

                    <el-table-column label="用户" prop="user_id">
                        <template #default="{ row }">
                            <div>
                                <el-avatar :src="row.user.avatar" :size="50" />
                            </div>

                            <span class="ml-2">{{ row.user.nickname }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column label="购买服务" prop="name" width="300px">
                        <template #default="{ row }">
                            <div class="" v-for="item in row.policy">
                                <div>
                                    <span>名称: {{ item.name }}</span>

                                    <span> 价格：{{ item.price }}</span>
                                    <span> 次数：{{ item.total_num }}</span>
                                </div>
                                <div>
                                    <span> 有效期：{{ item.start_date }} - {{ item.end_date }} </span>
                                </div>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="串号" prop="imei">
                        <template #default="{ row }">
                            <div class="flex flex-col">
                                <span>IMEI: {{ row.imei }}</span>
                                <span>机型: {{ row.policy[0].spec }}</span>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="支付信息" prop="imei">
                        <template #default="{ row }">
                            <div class="flex flex-col">
                                <span>支付方式：{{ row.pay_type == 1 ? '微信' : '其他' }}</span>
                                <span>支付时间：{{ row.pay_time }}</span>
                                <span>支付金额：{{ row.pay_price }}</span>
                                <span>交易号：{{ row.transaction_id }}</span>
                                <span>退款订单号：{{ row.refund_no }}</span>

                            </div>
                        </template>
                    </el-table-column>

                    <el-table-column label="状态" prop="status_text">
                        <template #default="{ row }">
                            <el-tag v-if="row.pay_status == 0" type="danger">待付款</el-tag>
                            <el-tag v-if="row.pay_status == 1" type="success"> 已付款</el-tag>
                            <el-tag v-if="row.pay_status == -10"> 已退款</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="卡密" prop="card_pwd" />
                    <el-table-column label="图片" prop="imgs">
                        <template #default="{ row }">
                            <div class="flex flex-wrap">
                                <el-image v-for="(item, index) in row.imgs_arr" :key="index"
                                    :preview-src-list="row.imgs_arr" :src="item" fit="fill" :lazy="true"
                                    :preview-teleported="true" style="width: 40px; height: 40px" />
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                            <el-button v-if="row.pay_status == 1 && row.insurance_policy_status == 0" type="primary"
                                link @click="refund(row)"> 退款
                            </el-button>
                            <el-button v-if="row.pay_status == 1 && row.insurance_policy_status == 0" type="success"
                                link @click="pass(row)"> 通过
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup name="insurancePolicyLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiInsurancePolicyLists, apiInsurancePolicyDelete, apiInsuranceApply } from '@/api/insurance_policy'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)

// 查询条件
const queryParams = reactive({
    status: '',
    order_no: '',
    spec: '',
    brand: '',
    imei: ''
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('insurance_policy')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiInsurancePolicyLists,
    params: queryParams
})

const pass = async (row: any) => {
    feedback.loading('处理中')

    await apiInsuranceApply({
        id: row.id,
        apply_status: 1
    })
    feedback.closeLoading()
    getLists()
}

const refund = async (row: any) => {
    await apiInsuranceApply({
        id: row.id,
        apply_status: 0
    })
    getLists()
}

// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await apiInsurancePolicyDelete({ id })
    getLists()
}

getLists()
</script>
